package com.guigu.controller.manufacture;


import com.guigu.pojo.design.DFile;
import com.guigu.pojo.manufacture.MDesignProcedure;
import com.guigu.pojo.manufacture.MDesignProcedureDetails;
import com.guigu.pojo.manufacture.MDesignProcedureModule;
import com.guigu.pojo.stock.SysUsers;
import com.guigu.service.design.DFileService;
import com.guigu.service.manufacture.MDesignProcedureDetailsService;
import com.guigu.service.manufacture.MDesignProcedureModuleService;
import com.guigu.service.manufacture.MDesignProcedureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/m-design-procedure-module")
public class MDesignProcedureModuleController {

    @Autowired
    private MDesignProcedureModuleService mDesignProcedureModuleService;

    @Autowired
    private MDesignProcedureService mDesignProcedureService;

    @Autowired
    private MDesignProcedureDetailsService mDesignProcedureDetailsService;

    @Autowired
    private DFileService dFileService;

    //工序物料设计单变更
    @GetMapping("/ChangProcessMaterialDesign")
    public String changProcessMaterialDesign() {
        return "jsp/manufacture/c/Chang_Process_material_design";
    }

    //工序物料设计单变更子表
    @GetMapping("/ChangProcessMaterialDesignSheet")
    public String changProcessMaterialDesignSheet() {
        return "jsp/manufacture/c/Chang_Process_material_design_sheet";
    }

    @GetMapping("/ChangProcessMaterialDesignSheetSub")
    public String changProcessMaterialDesignSheetSub() {
        return "jsp/manufacture/c/Chang_Process_material_design_sheet_sub";
    }

    @GetMapping("/ChangeProcessMaterialQuery")
    public String changeProcessMaterialQuery() {
        return "jsp/manufacture/c/Change_Process_material_query";
    }

    @GetMapping("/FormulateMaterialDesign")
    public String formulateMaterialDesign() {
        return "jsp/manufacture/c/Formulate_material_design";
    }

    @GetMapping("/InquiryProductMaterialDesign")
    public String inquiryProductMaterialDesign() {
        return "jsp/manufacture/c/Inquiry_Product_material_design";
    }

    @GetMapping("/ProcessMaterialDesignPreview")
    public String processMaterialDesignPreview() {
        return "jsp/manufacture/c/Process_material_design_preview";
    }

    @GetMapping("/ProcessMaterialDesignReview")
    public String processMaterialDesignReview() {
        return "jsp/manufacture/c/Process_material_design_review";
    }

    @GetMapping("/ProcessMaterialDesignSheet")
    public String processMaterialDesignSheet() {
        return "jsp/manufacture/c/Process_material_design_sheet";
    }

    @GetMapping("/ProcessMaterialDesignSheetReview")
    public String processMaterialDesignSheetReview() {
        return "jsp/manufacture/c/Process_material_design_sheet_review";
    }

    @GetMapping("/ProcessMaterialDesignSheetReviewSub")
    public String processMaterialDesignSheetReviewSub() {
        return "jsp/manufacture/c/Process_material_design_sheet_review_sub";
    }

    @GetMapping("/ProcessMaterialDesignSub")
    public String processMaterialDesignSub() {
        return "jsp/manufacture/c/Process_material_design_sub";
    }

    @GetMapping("/QueryProcessMaterialQuery")
    public String queryProcessMaterialQuery() {
        return "jsp/manufacture/c/Query_Process_material_query";
    }

    @GetMapping("/initialDesignSheet")
    @ResponseBody
    public MDesignProcedure initialDesignSheet(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        MDesignProcedure mDesignProcedure = mDesignProcedureService.selectById(id);
        return mDesignProcedure;
    }

    @PostMapping("/designProcedure")
    @ResponseBody
    public Boolean designProcedure(@RequestBody ArrayList<Object> list,HttpServletRequest req){
        System.out.println(list);
        String designId = "";
        String costPriceSum = "";
        String moduleCostPriceSum = "";

        HashMap<String, Object> map = (HashMap<String, Object>) list.get(0);
        if(map.containsKey("designId")){
            designId = map.get("designId").toString();
        }
        if(map.containsKey("costPriceSum")){
            costPriceSum = map.get("costPriceSum").toString();
        }
        if(map.containsKey("moduleCostPriceSum")){
            moduleCostPriceSum = map.get("moduleCostPriceSum").toString();
        }

        MDesignProcedure mDesignProcedure = mDesignProcedureService.selectByDesignId(designId);
        int j = 0;
        HashMap<String, Object> map1 = null;
        for (int i = 1;i<list.size();i++){
            map1 = (HashMap<String, Object>) list.get(i);

            ArrayList<MDesignProcedureDetails> mDesignProcedureDetails = mDesignProcedureDetailsService.selectArrByParentId(mDesignProcedure.getId());

            for (MDesignProcedureDetails mDesignProcedureDetail : mDesignProcedureDetails) {
                String procedureId = mDesignProcedureDetail.getProcedureId();
                Integer id = mDesignProcedureDetail.getId();
                if(map1.containsKey(procedureId)){
                    ArrayList<Object> arr= (ArrayList<Object>) map1.get(procedureId);
                    BigDecimal moduleSubtotal = BigDecimal.valueOf(0L);
                    for(int k = 0;k<arr.size();k++) {
                        j++;
                        HashMap<String ,Object> map2 = (HashMap<String, Object>) arr.get(k);
                        DFile dFile = dFileService.selectByProductId(map2.get("productId").toString());
                        MDesignProcedureModule mDesignProcedureModule = new MDesignProcedureModule();
                        mDesignProcedureModule.setParentId(id);
                        mDesignProcedureModule.setDetailsNumber(j);
                        mDesignProcedureModule.setProductId(dFile.getProductId());
                        mDesignProcedureModule.setProductName(dFile.getProductName());
                        mDesignProcedureModule.setType(dFile.getType());
                        BigDecimal amount = BigDecimal.valueOf(Long.parseLong(map2.get("amount").toString()));
                        mDesignProcedureModule.setAmount(amount);
                        mDesignProcedureModule.setProductDescribe(dFile.getProductDescribe());
                        mDesignProcedureModule.setAmountUnit(dFile.getAmountUnit());
                        mDesignProcedureModule.setCostPrice(dFile.getCostPrice());
                        BigDecimal subtotal = BigDecimal.valueOf(Long.parseLong(map2.get("amount").toString()) * dFile.getCostPrice().longValue());
                        mDesignProcedureModule.setSubtotal(subtotal);
                        moduleSubtotal = BigDecimal.valueOf(moduleSubtotal.longValue() + subtotal.longValue());
                        boolean save = mDesignProcedureModuleService.save(mDesignProcedureModule);
                        if(!save){
                            break;
                        }
                    }
                    mDesignProcedureDetail.setModuleSubtotal(moduleSubtotal);
                    HttpSession session = req.getSession();
                    SysUsers user = (SysUsers) session.getAttribute("user");
                    mDesignProcedureDetail.setRegister(user.getLoginId());
                    mDesignProcedureDetail.setRegisterTime(new Date());
                    mDesignProcedureDetail.setDesignModuleTag("1");
                    boolean b = mDesignProcedureDetailsService.updateById(mDesignProcedureDetail);
                    if(!b){
                        break;
                    }
                }
            }
        }
        BigDecimal ModuleCostPriceSum = BigDecimal.valueOf(Long.parseLong(moduleCostPriceSum));
        mDesignProcedure.setModuleCostPriceSum(ModuleCostPriceSum);
        mDesignProcedure.setDesignModuleTag("1");
        boolean b = mDesignProcedureService.updateById(mDesignProcedure);
        return b;
    }

    @GetMapping("/initialModule")
    @ResponseBody
    public ArrayList<MDesignProcedureModule> initialDetails(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("procedureId"))? Integer.parseInt(req.getParameter("procedureId")) : null;
        ArrayList<MDesignProcedureModule> mDesignProcedureModules = mDesignProcedureModuleService.selectArrByParentId(id);
        return mDesignProcedureModules;
    }

}

